package com.yxq.mdt.app.modules.video.adapter;


import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.convenientbanner.utils.ScreenUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.ui.activity.RechargeActivity;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.VideoInviteChatActivity;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.VoiceInviteChatActivity;
import com.yxq.mdt.app.modules.video.bean.ChatTabBean;
import com.yxq.mdt.app.modules.video.bean.MainVideoBean;
import com.yxq.mdt.app.modules.video.bean.VideoChatBean;
import com.yxq.mdt.app.modules.video.ui.activity.ChatClassifyDataAc;
import com.yxq.mdt.app.modules.video.ui.activity.DisplayVideoAc;
import com.yxq.mdt.app.utils.GridSpacingItemDecoration;

import java.util.List;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

public class ChatAdapter extends BaseMultiItemQuickAdapter<MainVideoBean, BaseViewHolder> {
    String localUserId;
    public CountDownTimer countDownTimer;

    public ChatAdapter(List<MainVideoBean> data,String userId) {
        super(data);
        addItemType(MainVideoBean.MALL_ITEM_TYPE_BANNER, R.layout.layout_item_nearby_list);
        addItemType(MainVideoBean.MALL_ITEM_TYPE_GRID_MENU, R.layout.layout_item_nearby_list);
        this.localUserId = userId;
    }

    @Override
    protected void convert(BaseViewHolder helper, MainVideoBean item) {
        switch (item.getItemType()) {
            case MainVideoBean.MALL_ITEM_TYPE_BANNER:
                RecyclerView recyclerView = helper.getView(R.id.recyclerView);
                recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
                ChatTabBean chatTabBean = ((ChatTabBean) item);
                ChatDataTabAdapter chatDataTabAdapter = new ChatDataTabAdapter(R.layout.channel_list_item, chatTabBean.getChatTabListData());
                recyclerView.setAdapter(chatDataTabAdapter);
                chatDataTabAdapter.setOnItemClickListener((adapter, view, position) -> {
                    Intent intent = new Intent(mContext, ChatClassifyDataAc.class);
                    intent.putExtra("id", chatTabBean.getChatTabListData().get(position).getId());
                    intent.putExtra("Text", chatTabBean.getChatTabListData().get(position).getText());
                    mContext.startActivity(intent);
                });
                break;
            case MainVideoBean.MALL_ITEM_TYPE_GRID_MENU://下面直播间
                RecyclerView menuView = helper.getView(R.id.recyclerView);
                menuView.setLayoutManager(new GridLayoutManager(mContext, 2));
                VideoChatBean videoChatBean = ((VideoChatBean) item);
                ChatDataListAdapter adapter = new ChatDataListAdapter(R.layout.item_chat_list, videoChatBean.getNavIconListBeanList());
                menuView.setAdapter(adapter);
                if (menuView.getItemDecorationCount() == 0) {
                    menuView.addItemDecoration(new GridSpacingItemDecoration(2, ScreenUtil.dip2px(mContext, 15), true));
                }
                adapter.setEmptyView(R.layout.layout_empty_view, menuView);
                adapter.isUseEmpty(!(adapter.getData().size() > 0));
                adapter.setOnItemChildClickListener((adapter1, view, position) -> {
                    switch (view.getId()) {
                        case R.id.open_chat_lint_one:
                            //个人主页
                            Intent intent = new Intent(mContext, SocialPersonalHomepageAc.class);
                            intent.putExtra("userId", videoChatBean.getNavIconListBeanList().get(position).getUserId());
                            startActivity(intent);
                            break;
                        case R.id.item_number:
                            //语音播放
                            break;
                        case R.id.item_number_video:
                            //视频播放
                            if (!TextUtils.isEmpty(videoChatBean.getNavIconListBeanList().get(position).getVideoDetails())) {
                                Intent videoIntent = new Intent(mContext, DisplayVideoAc.class);
                                videoIntent.putExtra("userId", videoChatBean.getNavIconListBeanList().get(position).getUserId());
                                videoIntent.putExtra("video", videoChatBean.getNavIconListBeanList().get(position).getVideoDetails());
                                videoIntent.putExtra("age", videoChatBean.getNavIconListBeanList().get(position).getAge());
                                videoIntent.putExtra("headimg", videoChatBean.getNavIconListBeanList().get(position).getHeadimg());
                                videoIntent.putExtra("sex", videoChatBean.getNavIconListBeanList().get(position).getSex());
                                videoIntent.putExtra("residence", videoChatBean.getNavIconListBeanList().get(position).getAddress());
                                videoIntent.putExtra("name", videoChatBean.getNavIconListBeanList().get(position).getName());
                                videoIntent.putExtra("money", videoChatBean.getNavIconListBeanList().get(position).getMoney());
                                startActivity(videoIntent);
                            } else {
                                Toast.makeText(mContext, "该用户暂未上传视频", Toast.LENGTH_LONG).show();
                            }
                            break;
                        case R.id.is_phone_layout:
                            //视频或者音频聊天
                            String userName = "yxq" + videoChatBean.getNavIconListBeanList().get(position).getUserId();
                            if (userName.equals("yxq" + localUserId)) {
                                ToastUtils.showShort("不能和自己通话");
                                return;
                            }
                            if (Constant.getSocialUser().getMoney() < videoChatBean.getNavIconListBeanList().get(position).getPrice()) {

                                CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                                        .setContentView(R.layout.dialog_clear_cache_layout)
                                        .setCancelable(true)
                                        .create();

                                TextView title = dialog.findViewById(R.id.tv_title);

                                title.setText("余额不足");

                                TextView content = dialog.findViewById(R.id.tv_content);
                                content.setText("当前余额不足，充值后可继续送礼，是否去充值？");

                                //充值
                                TextView sure = dialog.findViewById(R.id.tv_sure);
                                sure.setText("充值");
                                sure.setOnClickListener(v -> {
                                    Intent it = new Intent(mContext, RechargeActivity.class);
                                    startActivity(it);
                                    dialog.dismiss();
                                });

                                //取消
                                TextView cancel = dialog.findViewById(R.id.tv_cancel);
                                cancel.setText("取消");
                                cancel.setOnClickListener(v -> {
                                    dialog.dismiss();
                                });
                                dialog.show();
                                return;
                            }
                            if (videoChatBean.getNavIconListBeanList().get(position).getAnInt() == 1) {
                                //语音
                                //单参数display video
                                Intent goToChat = new Intent(mContext, VoiceInviteChatActivity.class);
                                goToChat.putExtra("userId", userName);
                                goToChat.putExtra("nickname", videoChatBean.getNavIconListBeanList().get(position).getName());
                                startActivity(goToChat);
                            } else {
                                //视频
                                //跳转视频聊天界面
                                Intent goToChat = new Intent(mContext, VideoInviteChatActivity.class);
                                goToChat.putExtra("userId", userName);
                                goToChat.putExtra("nickname", videoChatBean.getNavIconListBeanList().get(position).getName());
                                startActivity(goToChat);
                            }
                            break;
                        default:
                    }
                });
        }

    }

}

